package com.itany.controllers;


import com.itany.entity.ManagerUser;
import com.itany.entity.Role;
import com.itany.service.ManagerUserService;
import com.itany.vo.ActionResult;
import org.eclipse.jetty.client.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manageruser")
public class ManagerUserController {
    @Autowired
    ManagerUserService service;

    @RequestMapping("/login")
    @ResponseBody
    public ActionResult login(HttpServletRequest request, @RequestParam("username") String username, @RequestParam("password") String password) {
        ManagerUser managerUser = service.findByUsernameAndPassword(username, password);
        if (managerUser == null) {
            return ActionResult.fail("用户名或密码错误",401,null);
        } else {
            request.getSession().setAttribute("managerUser",managerUser);
            return ActionResult.success("登录成功",200,managerUser);
        }
    }

    @RequestMapping("/exit")
    public void exit(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("managerUser");
    }

    @RequestMapping("/modifyPassword")
    @ResponseBody
    public ActionResult modifyPassword(HttpServletRequest request,@RequestParam("password") String password){
        ManagerUser managerUser = (ManagerUser) request.getSession().getAttribute("managerUser");
        service.modifyPassword(managerUser.getId(),password);
        return ActionResult.success("修改成功",200,password);
    }

    @RequestMapping("/findAll")
    public Map<String,Object> getData(
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @RequestParam(name = "rows",defaultValue = "10") Integer rows,
            @RequestParam("name") String name){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Map<String,Object>> list = service.queryByParams(name);
        int total = list != null ? list.size() : 0;

        // 如果总数据量小于或等于每页显示的数据条数，则始终从第一页开始，且只显示一页
        if (total <= rows) {
            page = 1;
        } else {
            // 计算总页数，并检查请求的页码是否超出范围
            int totalPages = (int) Math.ceil((double) total / rows);
            if (page > totalPages) {
                page = 1; // 如果请求的页码大于总页数，则重置为第一页
            }
        }

        // 计算起始和结束索引
        int startIndex = (page - 1) * rows;
        int endIndex = Math.min(startIndex + rows, total);

        // 创建结果列表，并填充数据
        if(list!=null){
            List<Map<String, Object>> ref = list.subList(startIndex, endIndex);
            map.put("rows", ref);
        }else{
            map.put("rows",0);
        }

        map.put("total", total);

        return map;
    }

    @RequestMapping("/add")
    public ActionResult add(
            @RequestParam("password") String password,
            @RequestParam("name") String name,
            @RequestParam("id") Integer id,
            @RequestParam("ids") String ids) {
        service.insertManagerUser(password,name,id,ids);
        return ActionResult.success("添加成功!",200,null);
    }

    @RequestMapping("/findById")
    public ActionResult findById(@RequestParam("id") Integer id) {
        ManagerUser managerUser = service.queryById(id);
        return ActionResult.success("查找成功!",200,managerUser);
    }

    @RequestMapping("/modify")
    public ActionResult modify(
            @RequestParam("password") String password,
            @RequestParam("name") String name,
            @RequestParam("companyid") Integer companyid,
            @RequestParam("ids") String ids,
            @RequestParam("id") Integer id) {
        service.modifyManagerUser(password,name,companyid,ids,id);
        return ActionResult.success("修改成功!",200,null);
    }

    @RequestMapping("/findRoles")
    public ActionResult findRoles(@RequestParam("id") Integer id) {
        Map<String,Object> map = service.queryRoles(id);
        return ActionResult.success("查找成功!",200,map);
    }

    @RequestMapping("/findList")
    public ActionResult findList(@RequestParam("id") Integer id) {
        List<Map<String,Object>> data = service.queryList(id);
        return ActionResult.success("查找成功!",200,data);
    }
}
